<?php
namespace app\api\controller\xiservice;

use app\common\controller\xiservice\XiserviceApi;
use app\common\model\xiservice\City as CityModel;
use app\common\model\xiservice\Area as AreaModel;
use think\Db;
use think\Cache;
/**
 * 城市模块
 */
class City extends XiserviceApi
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * @ApiTitle    (获取开放城市接口)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/xiservice.city/list)
     * @ApiParams   (name="location", type="int", description="轮播位置 1=首页,2=分类")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'id':'int','name':'string','image':'string'}", description="返回数据")
     */
	public function getOpenCity()
	{
	   $list =CityModel::with(["area"])
                        ->where("status",1)
                        ->order("weigh desc")
                        ->select();
	   $this->success('查询成功',$list);
	}	


    /**
     * @ApiTitle    (获取定位信息)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/xiservice.city/list)
     * @ApiParams   (name="location", type="int", description="轮播位置 1=首页,2=分类")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'id':'int','name':'string','image':'string'}", description="返回数据")
     */
    public function location()
    {
       $lat = $this->request->request('lat'); 
       $lng = $this->request->request('lng'); 
       if(!$lat||!$lng){
         $this->success('查询成功',[]);
       }
       $data =AreaModel::where('level',2)->field("*,(6378.138 * 2 * asin(sqrt(pow(sin((lat * pi() / 180 - ".$lat." * pi() / 180) / 2),2) + cos(lat * pi() / 180) * cos(".$lat." * pi() / 180) * pow(sin((lng * pi() / 180 - ".$lng." * pi() / 180) / 2),2))) * 1000) as distance")
                        ->order('distance asc')
                        ->find();
       $this->success('查询成功',$data);
    }   


    public function getLetterCity()
    {
        $letter=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
        $all = count($letter);
        $rs =[];
        for($i=0;$i<$all;$i++)
        {
           $area = Db::query("select f.id,name,shortname from fa_xiservice_area f inner join fa_xiservice_city f1 on f1.city_id=f.id
and level=2 and name not like '%直辖%' and first=:first;",['first'=>$letter[$i]]);
          // $area = AreaModel::where("first",$letter[$i])->where('level',2)->where('name','not like','%直辖%')->field('id,name,shortname')->select();
          if($area)
          {
            $array=[
                "letter"=>$letter[$i],
                "list" =>$area
            ];
           array_push($rs, $array);
          }
        
        }
        $data = ["data"=>[]];
        $config = config('xiservice.order');
        if(isset($config['city_model']))
        {
            if($config['city_model']==0){
                 $data = ["data"=>$rs];
            }
           
        }
        $this->success(__('请求成功'), $data);  
    }

    public function searchCity()
    {
        $key = $this->request->request('key','');
        if($key==''){
            $this->error('请输入搜索内容', []);  
        }

        $data =Db::query("select f.id,name,shortname from fa_xiservice_area f inner join fa_xiservice_city f1 on f1.city_id=f.id
and level=2 and name like '%".$key."%'");
        // $data = areaModel::where("name","like","%".$key."%")
        //                     ->where('level',2)
        //                     ->field('id,name,shortname')
        //                     ->page($this->p,$this->r)
        //                     ->select();
        $this->success(__('请求成功'), $data);  
    }

}